﻿#include "deviceconfigtab.h"
#include "ui_deviceconfigtab.h"
#include "SendMessage/confirmdevice.h"
#include <QDebug>
#include <QMessageBox>
#include "Template/singleton.h"
#include <QFileDialog>


#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

DeviceConfigTab::DeviceConfigTab(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::DeviceConfigTab),
    confirmDevice(ConfirmDeviceInstance),
    m_timer_TSC(new QTimer(this))
{
    ui->setupUi(this);
    initDockConfig();
    connect(confirmDevice, SIGNAL(sendTypeTSC(Message_t)), this, SLOT(HandleTypeTSC(Message_t)));
    connect(this, SIGNAL(sendVerifyCoefficient(ModetoConfig)), confirmDevice, SLOT(recvMessageFromDeviceConfigTSC(ModetoConfig)));
    connect(confirmDevice, SIGNAL(sendFlashDataFromTSC(QByteArray)), this, SLOT(recvFlashDataFromTSC(QByteArray)));
    connect(m_timer_TSC, &QTimer::timeout, [=]() { confirmDevice->serial_T.write(QByteArray::fromHex("EB900700000000000000000000000000000000")); });
}

DeviceConfigTab::~DeviceConfigTab()
{
    delete ui;
}

void DeviceConfigTab::initDockConfig()
{

    this->setFeatures(QDockWidget::DockWidgetMovable);
    this->setAllowedAreas(Qt::RightDockWidgetArea);
    this->setContentsMargins(0, 0, 0, 0);
    //setFont(QFont("Microsoft YaHei UI", 8, 70));
    this->setWindowTitle(tr("T-SC"));
    // 设置状态栏黄色
    ui->m_labelStatus->setStyleSheet("QLabel{background:#ffff00; border-radius: 8px;}");
    // 时间编辑栏显示 年月日时分秒
    ui->m_dateTimeEdit->setDisplayFormat("yyyy-MM-dd HH:mm:ss");
    ui->m_dateTimeEdit_End->setDisplayFormat("yyyy-MM-dd HH:mm:ss");
    ui->m_spinBox_Hour->setRange(0, 17);
    ui->m_spinBox_Minute->setRange(0, 59);
    ui->m_spinBox_Second->setRange(0, 59);
    ui->m_proBar_Memory->setStyleSheet("QProgressBar{border-radius:5px;border:1px;background:rgb(0, 255, 0);} QProgressBar::chunk{border-radius:5px;background:rgb(255, 127, 0)}");

    ui->m_proBar_Memory->setFormat(QString("已使用内存 %%1").arg((100 * m_iPageQuantityTSC / 8192) == 0 ? 1 : (100 * m_iPageQuantityTSC / 8192)));

    ui->m_proBar_Memory->setAlignment(Qt::AlignLeft | Qt::AlignCenter);
    // 第三页 参数 下拉框选中第一个
    ui->m_combomBox_Coefficient->setCurrentIndex(0);

    // 设置TableWidget不可编辑
    for (int index = 0; index < 4; index++)
    {
        ui->m_tableWidget_CalibrationT->item(index, 0)->setFlags(ui->m_tableWidget_CalibrationT->item(index, 0)->flags() & ~Qt::ItemIsEditable);
        //  设置字体
        // ui->m_tableWidget_CalibrationT->item(index,0)->setFont(QFont("微软雅黑",9,QFont::Normal));
        // 设置字体颜色
        // ui->m_tableWidget_CalibrationT->item(index,0)->setTextColor(Qt::darkGray);
        // 设置 背景色
        // ui->m_tableWidget_CalibrationT->item(index,0)->setBackground(QBrush(QColor(Qt::lightGray)));
    }
    ui->m_tableWidget_CalibrationT->setFocusPolicy(Qt::NoFocus);
    //表头标题用QStringList来表示
    QStringList headerText;
    headerText << "标 签" << "数 值";
    ui->m_tableWidget_CalibrationT->setHorizontalHeaderLabels(headerText);
    ui->m_tableWidget_CalibrationT->setColumnWidth(1, 230);
    //设置最后一栏自适应长度
    ui->m_tableWidget_CalibrationT->horizontalHeader()->setStretchLastSection(true);

    ui->tabWidget->setTabText(0, "配置");
    ui->tabWidget->setTabText(1, "信息");
    ui->tabWidget->setTabText(2, "校准");
    ui->tabWidget->setTabText(3, "参数");
    // 设置按钮不可用
    ui->tabWidget->setTabEnabled(3, false);
    ui->tabWidget->setTabToolTip(3, QString("正在开发中"));
    ui->tabWidget->setCurrentIndex(0);

}

void DeviceConfigTab::HandleTypeTSC(Message_t stuMsg)
{
    if (stuMsg.deviceType == "5443")
    {
        switch (stuMsg.endWithType)
        {
        case 474946:
            this->HandDeviceInfo_TSC(stuMsg.arrData, "5443");
            break;

        case 474256:
            this->HandBattery_TSC(stuMsg.arrData, "5443");
            break;

        case 475449:
            this->HandSampleConfig_TSC(stuMsg.arrData, "5443");
            break;

        case 47434:
            this->HandTemperatureCoefficient_TSC(stuMsg.arrData);
            break;

        case 535444:
            this->HandSyncTime_TSC(stuMsg.arrData);
            break;

        case 534354:
            this->HandCoefficientTimeTSC(stuMsg.arrData, "5443");
            break;

        case 535350:
            this->HandSampleAfterDataTSC(stuMsg.arrData, "5443");
            break;

        case 46454:
            this->setProgressValue();
            break;

        case 737461:
            this->setStartSample();
            break;

        default:
            break;
        }
    }

}

void DeviceConfigTab::HandDeviceInfo_TSC(QByteArray data, QByteArray flag)
{
    if (data.endsWith("474946") && flag == "5443")
    {
        // 先确保QByteArray 是16进制，再转化为ascill
        QByteArray  DeviceName = QByteArray::fromHex(data.mid(0, 12)).data();
        if (DeviceName.mid(0, 2) == "TC")
        {
            ui->label_type->setText("OceanPlot T-SC");
        }
        QByteArray  HardWare = QByteArray::fromHex(data.mid(12, 6)).data();
        ui->m_labelHardWare->setText(HardWare);
        ui->m_labelSoftwareVersion->setText("1.6");
        ui->m_labelSerial->setText(data.mid(18, 6));

        //qDebug() << "页数=" << data.mid(28, 4);
        LocalDateTime = "20" + data.mid(32, 2) + "-" + data.mid(34, 2) + "-" + data.mid(36, 2) + " " + data.mid(38, 2) + ":" + data.mid(40, 2) + ":" + data.mid(42, 2);
        // 设置时间
        ui->m_labelDeviceTime->setText(LocalDateTime);

        QByteArray labStatus = data.mid(24, 4);

        if (labStatus == "0100")
        {
            ui->m_labelStatus->setText(tr(" 已启用,等待开始"));
            setSampleStatusOne(false);
            m_Start_Simple = false;
            ui->m_pushButton_StartSample->setText(" 停止");
            emit sendIsEnableRotateButton(false);
        }
        else if (labStatus == "0200")
        {
            ui->m_labelStatus->setText(tr(" 采样中"));
            ui->m_pushButton_StartSample->setText("停止");
            ui->m_pushButton_StartSample->setEnabled(true);
            m_Start_Simple = false;
            setSampleStatusTwo(false);
            // 让下面的开启轮训按钮灰掉
            emit sendIsEnableRotateButton(false);
        }
        else if (labStatus == "0401")
        {
            ui->m_labelStatus->setText(tr(" 按计划采样完毕"));
            emit sendIsEnableRotateButton(true);
        }

        else if (labStatus == "0402")
        {
            ui->m_labelStatus->setText(tr(" 用户终止"));

            setSampleStatusFlagThird(true);
            m_Start_Simple = true;
            ui->m_pushButton_StartSample->setText("启用");
            ui->m_pushButton_StartSample->setEnabled(true);
            emit sendIsEnableRotateButton(true);
        }

        else if (labStatus == "0403")
        {
            ui->m_labelStatus->setText(tr(" flash已存满"));
            emit sendIsEnableRotateButton(false);
        }
        m_iPageQuantityTSC = data.mid(28, 4).toUInt(nullptr, 16);
        ui->m_proBar_Memory->setValue(100 * m_iPageQuantityTSC / 8192);
    }
}

void DeviceConfigTab::setSampleStatusOne(bool flag)
{
    ui->m_Button_SyncLocalTime->setEnabled(flag);
    ui->m_dateTimeEdit->setEnabled(flag);
    ui->m_dateTimeEdit_End->setEnabled(flag);
    ui->m_radio_LocalDateTime->setEnabled(flag);
    ui->m_radioButton_Led->setEnabled(flag);
    ui->m_pbn_Save_Config->setEnabled(flag);
    ui->m_Use_Last_Config->setEnabled(flag);

    ui->m_spinBox_Hour->setEnabled(flag);
    ui->m_spinBox_Minute->setEnabled(flag);
    ui->m_spinBox_Second->setEnabled(flag);
    ui->m_radioButton_Hour->setEnabled(flag);
    ui->m_radioButton_Minute->setEnabled(flag);
    ui->m_radioButton_Second->setEnabled(flag);

    ui->m_pbn_DownLoad->setEnabled(flag);
    ui->m_Ptn_SaveCoefficient->setEnabled(flag);
    ui->m_Calibration_Coefficient->setEnabled(flag);
    //m_start_rotate->setEnabled(flag);
}


void DeviceConfigTab::setSampleStatusTwo(bool flag)
{
    ui->m_Button_SyncLocalTime->setEnabled(flag);
    ui->m_dateTimeEdit->setEnabled(flag);
    ui->m_dateTimeEdit_End->setEnabled(flag);
    ui->m_radio_LocalDateTime->setEnabled(flag);
    ui->m_radioButton_Led->setEnabled(flag);
    ui->m_pbn_Save_Config->setEnabled(flag);
    ui->m_Use_Last_Config->setEnabled(flag);

    ui->m_spinBox_Hour->setEnabled(flag);
    ui->m_spinBox_Minute->setEnabled(flag);
    ui->m_spinBox_Second->setEnabled(flag);
    ui->m_radioButton_Hour->setEnabled(flag);
    ui->m_radioButton_Minute->setEnabled(flag);
    ui->m_radioButton_Second->setEnabled(flag);

    ui->m_pbn_DownLoad->setEnabled(flag);
    ui->m_Ptn_SaveCoefficient->setEnabled(flag);
    ui->m_Calibration_Coefficient->setEnabled(flag);
    //m_start_rotate->setEnabled(flag);
}

void DeviceConfigTab::setSampleStatusFlagThird(bool flag)
{
    ui->m_Button_SyncLocalTime->setEnabled(flag);
    // ui->m_dateTimeEdit->setEnabled(flag);
    ui->m_dateTimeEdit_End->setEnabled(flag);
    ui->m_radio_LocalDateTime->setEnabled(flag);
    ui->m_radioButton_Led->setEnabled(flag);
    ui->m_pbn_Save_Config->setEnabled(flag);
    ui->m_Use_Last_Config->setEnabled(flag);

    ui->m_radioButton_Hour->setEnabled(flag);
    ui->m_radioButton_Minute->setEnabled(flag);
    ui->m_radioButton_Second->setEnabled(flag);

    ui->m_pbn_DownLoad->setEnabled(flag);
    ui->m_Ptn_SaveCoefficient->setEnabled(flag);
    ui->m_Calibration_Coefficient->setEnabled(flag);
}

void DeviceConfigTab::setSampleStatusFlagFour(bool flag)
{
    ui->m_Button_SyncLocalTime->setEnabled(flag);
    ui->m_dateTimeEdit->setEnabled(flag);
    ui->m_dateTimeEdit_End->setEnabled(flag);
    ui->m_radio_LocalDateTime->setEnabled(flag);
    ui->m_radioButton_Led->setEnabled(flag);
    ui->m_pbn_Save_Config->setEnabled(flag);
    ui->m_Use_Last_Config->setEnabled(flag);

    ui->m_pbn_DownLoad->setEnabled(flag);
    ui->m_Ptn_SaveCoefficient->setEnabled(flag);
    ui->m_Calibration_Coefficient->setEnabled(flag);
}

void DeviceConfigTab::setSampleStatus(bool flag)
{
    ui->m_Button_SyncLocalTime->setEnabled(flag);
    ui->m_dateTimeEdit->setEnabled(flag);
    ui->m_dateTimeEdit_End->setEnabled(flag);
    ui->m_radio_LocalDateTime->setEnabled(flag);
    ui->m_radioButton_Led->setEnabled(flag);
    ui->m_pbn_Save_Config->setEnabled(flag);
    ui->m_Use_Last_Config->setEnabled(flag);

    ui->m_spinBox_Hour->setEnabled(flag);
    ui->m_spinBox_Minute->setEnabled(flag);
    ui->m_spinBox_Second->setEnabled(flag);
    ui->m_radioButton_Hour->setEnabled(flag);
    ui->m_radioButton_Minute->setEnabled(flag);
    ui->m_radioButton_Second->setEnabled(flag);
    ui->m_pbn_DownLoad->setEnabled(flag);
    ui->m_Ptn_SaveCoefficient->setEnabled(flag);
    ui->m_Calibration_Coefficient->setEnabled(flag);
    ui->m_pushButton_StartSample->setEnabled(flag);
}


void DeviceConfigTab::HandBattery_TSC(QByteArray data, QByteArray flag)
{
    if (data.endsWith("474256") && flag == "5443")
    {
        double value = ((double)(data.mid(0, 2).toInt(nullptr, 16) * 2)) / 100;
        QString str = QString::number(value, 'f', 2).append("V");
        ui->m_labelVoltage->setText(str);
        data.mid(2, 2) == "00" ? ui->m_radioButton_Led->setChecked(false) : ui->m_radioButton_Led->setChecked(true);
        QString CalibrationTime = "20" + QString("%1").arg(data.mid(4, 2).toInt(nullptr, 10), 2, 10, QChar('0')) + "-" + QString("%1").arg(data.mid(6, 2).toInt(nullptr, 10), 2, 10, QChar('0')) + "-" + QString("%1").arg(data.mid(8, 2).toInt(nullptr, 10), 2, 10, QChar('0'))
            + " " + QString("%1").arg(data.mid(10, 2).toInt(nullptr, 10), 2, 10, QChar('0')) + ":" + QString("%1").arg(data.mid(12, 2).toInt(nullptr, 10), 2, 10, QChar('0')) + ":" + QString("%1").arg(data.mid(14, 2).toInt(nullptr, 10), 2, 10, QChar('0'));
        ui->m_groupBox_CalibrationDate->setTitle("校准日期:" + CalibrationTime);
    }
}


void DeviceConfigTab::HandSampleConfig_TSC(QByteArray data, QByteArray flag)
{
    if (data.endsWith("475449") && flag == "5443")
    {
        QString startData = "20" + data.mid(2, 2) + "-" + data.mid(4, 2) + "-" + data.mid(6, 2) + " " + data.mid(8, 2) + ":" + data.mid(10, 2) + ":" + data.mid(12, 2);
        QDateTime time = QDateTime::fromString(startData, "yyyy-MM-dd hh:mm:ss");
        ui->m_dateTimeEdit->setDateTime(time);
        QString endDate = "20" + data.mid(14, 2) + "-" + data.mid(16, 2) + "-" + data.mid(18, 2) + " " + data.mid(20, 2) + ":" + data.mid(22, 2) + ":" + data.mid(24, 2);
        time = QDateTime::fromString(endDate, "yyyy-MM-dd hh:mm:ss");
        ui->m_dateTimeEdit_End->setDateTime(time);
        // 说明是秒
        if (data.mid(30, 2) == "fa")
        {
            int ret = data.mid(28, 2).toInt();
            ui->m_spinBox_Second->setValue(100 - ret);

            m_iInterval = 100 - ret;
            ui->m_spinBox_Minute->setValue(0);
            ui->m_spinBox_Hour->setValue(0);

            ui->m_spinBox_Second->setEnabled(true);
            ui->m_spinBox_Minute->setEnabled(false);
            ui->m_spinBox_Hour->setEnabled(false);

            ui->m_radioButton_Second->setChecked(true);
            ui->m_radioButton_Minute->setChecked(false);
            ui->m_radioButton_Hour->setChecked(false);
        }
        // 说明是分钟
        else if (data.mid(30, 2) == "fb")
        {
            int sampleInterval = data.mid(26, 4).toInt(nullptr, 16);
            int iHour = sampleInterval / 60;
            int iMin = sampleInterval - iHour * 60;

            if (iHour != 0)
            {
                ui->m_spinBox_Second->setValue(0);
                ui->m_spinBox_Minute->setValue(iMin);
                ui->m_spinBox_Hour->setValue(iHour);

                m_iInterval = (iHour * 60 + iMin) * 60;

                ui->m_radioButton_Second->setChecked(false);
                ui->m_radioButton_Minute->setChecked(false);
                ui->m_radioButton_Hour->setChecked(true);

                ui->m_spinBox_Second->setEnabled(false);
                ui->m_spinBox_Minute->setEnabled(true);
                ui->m_spinBox_Hour->setEnabled(true);
            }
            else {
                // 只有分钟
                ui->m_spinBox_Second->setValue(0);
                ui->m_spinBox_Minute->setValue(iMin);
                ui->m_spinBox_Hour->setValue(0);

                m_iInterval = iMin * 60;

                ui->m_radioButton_Second->setChecked(false);
                ui->m_radioButton_Minute->setChecked(true);
                ui->m_radioButton_Hour->setChecked(false);

                ui->m_spinBox_Second->setEnabled(false);
                ui->m_spinBox_Minute->setEnabled(true);
                ui->m_spinBox_Hour->setEnabled(false);
            }
        }
        m_bIM = "00" == data.mid(0, 2) ? true : false;
        data.mid(0, 2) == "00" ? ui->m_radio_LocalDateTime->setChecked(true) : ui->m_radio_LocalDateTime->setChecked(false);
    }
}

void DeviceConfigTab::HandTemperatureCoefficient_TSC(QByteArray data)
{
    if (data.contains("47434"))
    {
        QByteArray m_T0 = QByteArray::fromHex(data.mid(0, 16)).data();
        QByteArray arrTemp0;
        for (auto& byData : m_T0)
        {
            arrTemp0.push_front(byData);
        }
        memcpy(&m_TSC[0], arrTemp0.data(), sizeof(double));

        QByteArray m_T1 = QByteArray::fromHex(data.mid(16, 16)).data();
        QByteArray arrTemp1;
        for (auto& byData : m_T1)
        {
            arrTemp1.push_front(byData);
        }
        memcpy(&m_TSC[1], arrTemp1.data(), sizeof(double));

        QByteArray m_T2 = QByteArray::fromHex(data.mid(32, 16)).data();
        QByteArray arrTemp2;
        for (auto& byData : m_T2)
        {
            arrTemp2.push_front(byData);
        }
        memcpy(&m_TSC[2], arrTemp2.data(), sizeof(double));

        QByteArray m_T3 = QByteArray::fromHex(data.mid(48, 16)).data();
        QByteArray arrTemp3;
        for (auto& byData : m_T3)
        {
            arrTemp3.push_front(byData);
        }
        memcpy(&m_TSC[3], arrTemp3.data(), sizeof(double));
    }
    ui->m_combomBox_Coefficient->removeItem(1);
    ui->m_tableWidget_CalibrationT->setItem(0, 1, new QTableWidgetItem(QString("%1").arg(m_TSC[0], 0, 'R', 15)));
    ui->m_tableWidget_CalibrationT->setItem(1, 1, new QTableWidgetItem(QString("%1").arg(m_TSC[1], 0, 'R', 15)));
    ui->m_tableWidget_CalibrationT->setItem(2, 1, new QTableWidgetItem(QString("%1").arg(m_TSC[2], 0, 'R', 15)));
    ui->m_tableWidget_CalibrationT->setItem(3, 1, new QTableWidgetItem(QString("%1").arg(m_TSC[3], 0, 'R', 15)));

}

void DeviceConfigTab::HandSyncTime_TSC(QByteArray data)
{
    if (data.contains("535444"))
    {
        LocalDateTime = "20" + data.mid(0, 2) + "-" + data.mid(2, 2) + "-" + data.mid(4, 2) + " " + data.mid(6, 2) + ":" + data.mid(8, 2) + ":" + data.mid(10, 2);
        ui->m_labelDeviceTime->setText(LocalDateTime);
    }
}

void DeviceConfigTab::HandCoefficientTimeTSC(QByteArray data, QByteArray flag)
{
    if (data.endsWith("534354") && flag == "5443")
    {
        // qDebug() << "111111111" << data;
        // QString CalibrationTime = "20" + QString("%1").arg(data.mid(0, 2).toInt(nullptr, 16), 2, 10, QChar('0')) + "-" + QString("%1").arg(data.mid(2, 2).toInt(nullptr, 16), 2, 10, QChar('0')) + "-" + QString("%1").arg(data.mid(4, 2).toInt(nullptr, 16), 2, 10, QChar('0'))
        //    + " " + QString("%1").arg(data.mid(6, 2).toInt(nullptr, 16), 2, 10, QChar('0')) + ":" + QString("%1").arg(data.mid(8, 2).toInt(nullptr, 16), 2, 10, QChar('0')) + ":" + QString("%1").arg(data.mid(10, 2).toInt(nullptr, 16), 2, 10, QChar('0'));
        QString CalibrationTime = "20" + data.mid(0, 2) + "-" + data.mid(2, 2) + "-" + data.mid(4, 2)
            + " " + data.mid(6, 2) + ":" + data.mid(8, 2) + ":" + data.mid(10, 2);
        ui->m_groupBox_CalibrationDate->setTitle("校准日期:" + CalibrationTime);
    }
}

void DeviceConfigTab::HandSampleAfterDataTSC(QByteArray data, QByteArray flag)
{
    if (data.endsWith("535350") && flag == "5443")
    {
        // qDebug() << "11111111111"<<data;
        // 分为FA(秒) 和FB(分钟) 两种
        if (data.mid(18, 2) == "fa")
        {
            int ret = data.mid(16, 2).toInt();
            ui->m_spinBox_Second->setValue(100 - ret);

            m_iInterval = 100 - ret;

            ui->m_spinBox_Minute->setValue(0);
            ui->m_spinBox_Hour->setValue(0);

            ui->m_spinBox_Second->setEnabled(true);
            ui->m_spinBox_Minute->setEnabled(false);
            ui->m_spinBox_Hour->setEnabled(false);

            ui->m_radioButton_Second->setChecked(true);
            ui->m_radioButton_Minute->setChecked(false);
            ui->m_radioButton_Hour->setChecked(false);
        }
        // 说明是分钟
        else if (data.mid(18, 2) == "fb")
        {
            int sampleInterval = data.mid(14, 4).toInt(nullptr, 16);
            int iHour = sampleInterval / 60;
            int iMin = sampleInterval - iHour * 60;

            if (iHour != 0)
            {
                ui->m_spinBox_Second->setValue(0);
                ui->m_spinBox_Minute->setValue(iMin);
                ui->m_spinBox_Hour->setValue(iHour);

                m_iInterval = (iHour * 60 + iMin) * 60;

                ui->m_radioButton_Second->setChecked(false);
                ui->m_radioButton_Minute->setChecked(false);
                ui->m_radioButton_Hour->setChecked(true);

                ui->m_spinBox_Second->setEnabled(false);
                ui->m_spinBox_Minute->setEnabled(true);
                ui->m_spinBox_Hour->setEnabled(true);
            }
            else {
                // 只有分钟
                ui->m_spinBox_Second->setValue(0);
                ui->m_spinBox_Minute->setValue(iMin);
                ui->m_spinBox_Hour->setValue(0);

                m_iInterval = iMin * 60;

                ui->m_radioButton_Second->setChecked(false);
                ui->m_radioButton_Minute->setChecked(true);
                ui->m_radioButton_Hour->setChecked(false);

                ui->m_spinBox_Second->setEnabled(false);
                ui->m_spinBox_Minute->setEnabled(true);
                ui->m_spinBox_Hour->setEnabled(false);
            }

        }
        data.mid(0, 2) == "00" ? ui->m_radio_LocalDateTime->setChecked(true) : ui->m_radio_LocalDateTime->setChecked(false);

        QString startDate = "20" + data.mid(2, 2) + "-" + data.mid(4, 2) + "-" + data.mid(6, 2) + " " + data.mid(8, 2) + ":" + data.mid(10, 2) + ":" + data.mid(12, 2);
        QDateTime time = QDateTime::fromString(startDate, "yyyy-MM-dd hh:mm:ss");
        ui->m_dateTimeEdit->setDateTime(time);
        QString endDate = "20" + data.mid(20, 2) + "-" + data.mid(22, 2) + "-" + data.mid(24, 2) + " " + data.mid(26, 2) + ":" + data.mid(28, 2) + ":" + data.mid(30, 2);
        time = QDateTime::fromString(endDate, "yyyy-MM-dd hh:mm:ss");
        ui->m_dateTimeEdit_End->setDateTime(time);
        //setSampleStatusOne(false);

    }

}

void DeviceConfigTab::setProgressValue()
{
    // 清空flash之后，设置进度条
    ui->m_proBar_Memory->setValue(0);
    // 然后下发LED是否启动命令
    arrLED = ui->m_radioButton_Led->isChecked() ? "01" : "00";

    confirmDevice->serial_T.write(QByteArray::fromHex("EB9016" + arrLED + "000000000000000000000000000000"));  // 534643
    qDebug() << "111111111" << "EB9016" + arrLED + "000000000000000000000000000000";
}

void DeviceConfigTab::setStartSample()
{
    // 下发采样指令
    uint iTotalSec = 0;
    QByteArray arrTemp;
    // 秒 100-值，没有转16进制
    if (ui->m_radioButton_Second->isChecked())
    {
        if (ui->m_spinBox_Second->value() == 0)
        {
            QMessageBox::information(this, "提示信息", "选择秒时间等于0,请重新选择", QMessageBox::Ok);
            return;
        }
        iTotalSec += ui->m_spinBox_Second->value();
        QString str = QString::number(100 - iTotalSec);
        arrTemp.append("00" + str + "FA");
    }
    // 小时选中了，分钟也可以选
    if (ui->m_radioButton_Hour->isChecked())
    {
        iTotalSec = ui->m_spinBox_Hour->value();
        if (iTotalSec != 0)
        {
            iTotalSec = iTotalSec * 60;
            iTotalSec += ui->m_spinBox_Minute->value();
        }
        else {
            iTotalSec = ui->m_spinBox_Minute->value();
            if (iTotalSec == 0)
            {
                QMessageBox::information(this, "提示信息", "时间间隔为0,请重新选择", QMessageBox::Ok);
                return;
            }
        }
        QString str = QString("%1").arg(iTotalSec, 4, 16, QLatin1Char('0')).toUpper();
        arrTemp.append(str + "FB");
    }

    // 分钟的radioButton被选中只有分钟
    if (ui->m_radioButton_Minute->isChecked())
    {
        if (ui->m_spinBox_Minute->value() == 0)
        {
            QMessageBox::information(this, "提示信息", "选择分钟的时间等于0,请重新选择", QMessageBox::Ok);
            return;
        }
        iTotalSec += ui->m_spinBox_Minute->value();
        QString str = QString("%1").arg(iTotalSec, 4, 16, QLatin1Char('0')).toUpper();
        arrTemp.append(str + "FB");
    }
    // 用户是否立即采样
    QByteArray arrAA = ui->m_radio_LocalDateTime->isChecked() ? "00" : "01";
    if (arrAA == "00")
    {
        ui->m_dateTimeEdit->setDateTime(QDateTime::fromString(LocalDateTime, "yyyy-MM-dd hh:mm:ss"));
    }
    QByteArray arrStartDataTime = ui->m_radio_LocalDateTime->isChecked() ? ui->m_labelDeviceTime->text().mid(2).remove('-').remove(':').remove(' ').toUtf8() : ui->m_dateTimeEdit->dateTime().toString("yyMMddhhmmss").toUtf8();
    QByteArray arrEndDateTime = ui->m_dateTimeEdit_End->dateTime().toString("yyMMddhhmmss").toUtf8();
    QByteArray sendData = "EB9003" + arrAA + arrStartDataTime + arrTemp + arrEndDateTime;
    confirmDevice->serial_T.write(QByteArray::fromHex(sendData));  // 535350
    qDebug() << "11111111111111" << sendData;
}



void DeviceConfigTab::on_m_Ptn_SaveCoefficient_clicked()
{
    QByteArray arrDataAB;
    QByteArray arrSendAB;
    double dT0 = ui->m_tableWidget_CalibrationT->item(0, 1)->text().toDouble();
    double dT1 = ui->m_tableWidget_CalibrationT->item(1, 1)->text().toDouble();

    arrDataAB.resize(2 * sizeof(double));
    memcpy(arrDataAB.data(), &dT1, sizeof(double));
    memcpy(arrDataAB.data() + sizeof(double), &dT0, sizeof(double));
    for (auto byte : arrDataAB)
    {
        arrSendAB.push_front(byte);
    }
    QByteArray arrDataCD;
    QByteArray arrSendCD;
    double dT2 = ui->m_tableWidget_CalibrationT->item(2, 1)->text().toDouble();
    double dT3 = ui->m_tableWidget_CalibrationT->item(3, 1)->text().toDouble();

    arrDataCD.resize(2 * sizeof(double));
    memcpy(arrDataCD.data(), &dT3, sizeof(double));
    memcpy(arrDataCD.data() + sizeof(double), &dT2, sizeof(double));
    for (auto byte : arrDataCD)
    {
        arrSendCD.push_front(byte);
    }
    ModetoConfig mode;
    mode.arrDataA = arrSendAB;
    mode.arrDataB = arrSendCD;
    mode.DateType = 1;
    emit sendVerifyCoefficient(mode);
}


void DeviceConfigTab::on_m_Calibration_Coefficient_clicked()
{
    confirmDevice->serial_T.write(QByteArray::fromHex("EB900600000000000000000000000000000000"));
}


void DeviceConfigTab::on_m_pushButton_StartSample_clicked()
{
    if (m_Start_Simple)
    {
        // 判断时间
        auto iCurTime = QDateTime::fromString(LocalDateTime, "yyyy-MM-dd hh:mm:ss").toTime_t();
        auto iStartDateTime = ui->m_dateTimeEdit->dateTime().toTime_t();
        auto iEndDateTime = ui->m_dateTimeEdit_End->dateTime().toTime_t();

        if (!ui->m_radio_LocalDateTime->isChecked() && iCurTime > iStartDateTime)
        {
            QMessageBox::information(this, "提示信息", "开始时间早于当前时间,请重新选择", QMessageBox::Ok);
            return;
        }
        // 格式化内存
        if (!ui->m_radio_LocalDateTime->isChecked() && iStartDateTime > iEndDateTime)
        {
            QMessageBox::information(this, "提示信息", "开始时间晚于结束时间,请重新选择", QMessageBox::Ok);
            return;
        }

        if (ui->m_radio_LocalDateTime->isChecked() && iStartDateTime > iEndDateTime)
        {
            QMessageBox::information(this, "提示信息", "开始时间早于结束时间,请重新选择", QMessageBox::Ok);
            return;
        }

        if (ui->m_radio_LocalDateTime->isChecked() && iEndDateTime < iCurTime)
        {
            QMessageBox::information(this, "提示信息", "结束时间晚于当前时间,请重新选择", QMessageBox::Ok);
            return;
        }

        uint iTotalSec = 0;
        QByteArray arrTemp;
        // 秒 100-值，没有转16进制
        if (ui->m_spinBox_Second->isEnabled())
        {
            if (ui->m_spinBox_Second->value() == 0)
            {
                QMessageBox::information(this, "提示信息", "时间间隔为0,请重新选择", QMessageBox::Ok);
                return;
            }
            iTotalSec += ui->m_spinBox_Second->value();
            QString str = QString::number(100 - iTotalSec);
            arrTemp.append("00" + str + "FA");
        }

        // 小时选中了，分钟也可以选
        else if (ui->m_spinBox_Hour->isEnabled())
        {
            iTotalSec = ui->m_spinBox_Hour->value();
            if (iTotalSec != 0)
            {
                iTotalSec = iTotalSec * 60;
                iTotalSec += ui->m_spinBox_Minute->value();
            }
            // 小时 等于 0的情况
            else {
                iTotalSec = ui->m_spinBox_Minute->value();
                if (iTotalSec == 0)
                {
                    QMessageBox::information(this, "提示信息", "时间间隔为0,请重新选择", QMessageBox::Ok);
                    return;
                }
            }
            QString str = QString("%1").arg(iTotalSec, 4, 16, QLatin1Char('0')).toUpper();
            arrTemp.append(str + "FB");
        }
        // 单独选择分钟
        if (ui->m_spinBox_Minute->isEnabled())
        {
            iTotalSec += ui->m_spinBox_Minute->value();
            if (iTotalSec == 0)
            {
                QMessageBox::information(this, "提示信息", "时间间隔为0,请重新选择", QMessageBox::Ok);
                return;
            }
            QString str = QString("%1").arg(iTotalSec, 4, 16, QLatin1Char('0')).toUpper();
            arrTemp.append(str + "FB");
        }

        if (iTotalSec == 0)
        {
            QMessageBox::information(this, "提示信息", "时间间隔为0,请重新选择", QMessageBox::Ok);
            return;
        }

        // 用户是否立即采样
        QByteArray arrAA = ui->m_radio_LocalDateTime->isChecked() ? "00" : "01";
        if (arrAA == "00")
        {
            ui->m_dateTimeEdit->setDateTime(QDateTime::fromString(LocalDateTime, "yyyy-MM-dd hh:mm:ss"));
        }
        // LED 是否启用
        arrLED = ui->m_radioButton_Led->isChecked() ? "01" : "00";
        QMessageBox::StandardButton result = QMessageBox::information(this, "提示信息", "是否清空内存", QMessageBox::Yes | QMessageBox::No);
        switch (result)
        {
        case QMessageBox::Yes:
            // 下发清空内存操作
            confirmDevice->serial_T.write(QByteArray::fromHex("EB901500000000000000000000000000000000"));  // 464d54
            m_Start_Simple = false;
            ui->m_pushButton_StartSample->setText("停止");
            break;

        case QMessageBox::No:
            return;
            break;
        default:
            break;
        }
    }
    else if (!m_Start_Simple)
    {
        m_Start_Simple = true;
        ui->m_pushButton_StartSample->setText("启用");

        ui->m_radioButton_Second->setChecked(true);
        ui->m_spinBox_Second->setEnabled(true);

        confirmDevice->serial_T.write(QByteArray::fromHex("EB900400000000000000000000000000000000"));
        QTimer::singleShot(100, this, [=]() {confirmDevice->serial_T.write(QByteArray::fromHex("EB900400000000000000000000000000000000")); });

    }
}


void DeviceConfigTab::on_m_pbn_Save_Config_clicked()
{
    // 下发采样指令
    uint iTotalSec = 0;
    QByteArray arrTemp;
    // 秒 100-值，没有转16进制
    if (ui->m_radioButton_Second->isChecked())
    {
        if (ui->m_spinBox_Second->value() == 0)
        {
            QMessageBox::information(this, "提示信息", "选择秒时间等于0,请重新选择", QMessageBox::Ok);
            return;
        }
        iTotalSec += ui->m_spinBox_Second->value();
        QString str = QString::number(100 - iTotalSec);
        arrTemp.append("00" + str + "FA");
    }
    // 小时选中了，分钟也可以选
    if (ui->m_radioButton_Hour->isChecked())
    {
        iTotalSec = ui->m_spinBox_Hour->value();
        if (iTotalSec != 0)
        {
            iTotalSec = iTotalSec * 60;
            iTotalSec += ui->m_spinBox_Minute->value();
        }
        else {
            iTotalSec = ui->m_spinBox_Minute->value();
            if (iTotalSec == 0)
            {
                QMessageBox::information(this, "提示信息", "时间间隔为0,请重新选择", QMessageBox::Ok);
                return;
            }
        }
        QString str = QString("%1").arg(iTotalSec, 4, 16, QLatin1Char('0')).toUpper();
        arrTemp.append(str + "FB");
    }

    // 分钟的radioButton被选中只有分钟
    if (ui->m_radioButton_Minute->isChecked())
    {
        if (ui->m_spinBox_Minute->value() == 0)
        {
            QMessageBox::information(this, "提示信息", "选择分钟的时间等于0,请重新选择", QMessageBox::Ok);
            return;
        }
        iTotalSec += ui->m_spinBox_Minute->value();
        QString str = QString("%1").arg(iTotalSec, 4, 16, QLatin1Char('0')).toUpper();
        arrTemp.append(str + "FB");
    }
    // 用户是否立即采样
    QByteArray arrAA = ui->m_radio_LocalDateTime->isChecked() ? "00" : "01";
    if (arrAA == "00")
    {
        ui->m_dateTimeEdit->setDateTime(QDateTime::fromString(LocalDateTime, "yyyy-MM-dd hh:mm:ss"));
    }
    QByteArray arrStartDataTime = ui->m_radio_LocalDateTime->isChecked() ? ui->m_labelDeviceTime->text().mid(2).remove('-').remove(':').remove(' ').toUtf8() : ui->m_dateTimeEdit->dateTime().toString("yyMMddhhmmss").toUtf8();
    QByteArray arrEndDateTime = ui->m_dateTimeEdit_End->dateTime().toString("yyMMddhhmmss").toUtf8();
    QByteArray sendData = "EB9003" + arrAA + arrStartDataTime + arrTemp + arrEndDateTime;
    
    QMessageBox::StandardButton result = QMessageBox::information(this, "提示信息", "是否清空内存", QMessageBox::Yes | QMessageBox::No);
    switch (result)
    {
    case QMessageBox::Yes:
        confirmDevice->serial_T.write(QByteArray::fromHex("EB901500000000000000000000000000000000"));   // 清内存
        // 然后下发停止采样指令
        QTimer::singleShot(700, this, [=]() {
            confirmDevice->serial_T.write(QByteArray::fromHex("EB900400000000000000000000000000000000"));   
            });
        QTimer::singleShot(800, this, [=]() {
            confirmDevice->serial_T.write(QByteArray::fromHex("EB900400000000000000000000000000000000"));   // 535450   
            });
        QTimer::singleShot(900, this, [=]() {
            confirmDevice->serial_T.write(QByteArray::fromHex("EB900400000000000000000000000000000000"));   // 535450   
            });
        // 下发清空内存操作
        break;
    case QMessageBox::No:
        return;
        break;
    default:
        break;
    }


#if 0
    // 判断时间
    auto iCurTime = QDateTime::fromString(LocalDateTime, "yyyy-MM-dd hh:mm:ss").toTime_t();
    auto iStartDateTime = ui->m_dateTimeEdit->dateTime().toTime_t();
    auto iEndDateTime = ui->m_dateTimeEdit_End->dateTime().toTime_t();

    if (!ui->m_radio_LocalDateTime->isChecked() && iCurTime > iStartDateTime)
    {
        QMessageBox::information(this, "提示信息", "开始时间大于当前时间,请重新选择", QMessageBox::Ok);
        return;
    }
    // 格式化内存
    if (!ui->m_radio_LocalDateTime->isChecked() && iStartDateTime > iEndDateTime)
    {
        QMessageBox::information(this, "提示信息", "开始时间大于结束时间,请重新选择", QMessageBox::Ok);
        return;
    }

    if (ui->m_radio_LocalDateTime->isChecked() && iStartDateTime > iEndDateTime)
    {
        QMessageBox::information(this, "提示信息", "开始时间大于结束时间,请重新选择", QMessageBox::Ok);
        return;
    }

    if (ui->m_radio_LocalDateTime->isChecked() && iEndDateTime < iCurTime)
    {
        QMessageBox::information(this, "提示信息", "结束时间小于当前时间,请重新选择", QMessageBox::Ok);
        return;
    }

    // 下发采样指令
    uint iTotalSec = 0;
    QByteArray arrTemp;
    // 秒 100-值，没有转16进制
    if (ui->m_spinBox_Second->isEnabled())
    {
        if (ui->m_spinBox_Second->value() == 0)
        {
            QMessageBox::information(this, "提示信息", "选择秒时间等于0,请重新选择", QMessageBox::Ok);
            return;
        }
        iTotalSec += ui->m_spinBox_Second->value();
        QString str = QString::number(100 - iTotalSec);
        arrTemp.append("00" + str + "FA");
    }
    // 小时选中了，分钟也可以选
    if (ui->m_radioButton_Hour->isChecked())
    {
        iTotalSec = ui->m_spinBox_Hour->value();
        if (iTotalSec != 0)
        {
            iTotalSec = iTotalSec * 60;
            iTotalSec += ui->m_spinBox_Minute->value();
        }
        else {
            iTotalSec = ui->m_spinBox_Minute->value();
            if (iTotalSec == 0)
            {
                QMessageBox::information(this, "提示信息", "时间间隔为0,请重新选择", QMessageBox::Ok);
                return;
            }
        }
        QString str = QString("%1").arg(iTotalSec, 4, 16, QLatin1Char('0')).toUpper();
        arrTemp.append(str + "FB");
    }

    // 分钟的radioButton被选中只有分钟
    if (ui->m_radioButton_Minute->isChecked())
    {
        if (ui->m_spinBox_Minute->value() == 0)
        {
            QMessageBox::information(this, "提示信息", "选择分钟的时间等于0,请重新选择", QMessageBox::Ok);
            return;
        }
        iTotalSec += ui->m_spinBox_Minute->value();
        QString str = QString("%1").arg(iTotalSec, 4, 16, QLatin1Char('0')).toUpper();
        arrTemp.append(str + "FB");
    }

    // 用户是否立即采样
    QByteArray arrAA = ui->m_radio_LocalDateTime->isChecked() ? "00" : "01";
    if (arrAA == "00")
    {
        ui->m_dateTimeEdit->setDateTime(QDateTime::fromString(LocalDateTime, "yyyy-MM-dd hh:mm:ss"));
    }
    // LED 是否启用
    arrLED = ui->m_radioButton_Led->isChecked() ? "01" : "00";

    QByteArray arrEndDateTime = ui->m_dateTimeEdit_End->dateTime().toString("yyMMddhhmmss").toUtf8();

    QByteArray arrStartDataTime = ui->m_radio_LocalDateTime->isChecked() ? ui->m_labelDeviceTime->text().mid(2).remove('-').remove(':').remove(' ').toUtf8() : ui->m_dateTimeEdit->dateTime().toString("yyMMddhhmmss").toUtf8();
    // QByteArray sendData = "EB9003" + arrAA + arrStartDataTime + arrTemp.toHex().toUpper() + "FA" + arrEndDateTime;
    QByteArray sendData = "EB9003" + arrAA + arrStartDataTime + arrTemp + arrEndDateTime;
    QMessageBox::StandardButton result = QMessageBox::information(this, "提示信息", "是否清空内存", QMessageBox::Yes | QMessageBox::No);
    ModetoConfig mode;
    mode.arrDataA = sendData;
    mode.arrDataB = arrLED;
    mode.DateType = 2;
    switch (result)
    {
    case QMessageBox::Yes:
        emit sendVerifyCoefficient(mode);
        QTimer::singleShot(800, this, [=]() {confirmDevice->serial_T.write(QByteArray::fromHex("EB900400000000000000000000000000000000")); });
    case QMessageBox::No:
        return;
        break;
    default:
        break;

    }
#endif
}


void DeviceConfigTab::on_m_pbn_DownLoad_clicked()
{
    m_timer_TSC->stop();

    confirmDevice->serial_T.write(QByteArray::fromHex("EB901000000000000000000000000000000000"));

    QTimer::singleShot(200, this, [=]() { 
        confirmDevice->serial_T.write(QByteArray::fromHex("EB900500000000000000000000000000000000"));
        });

    // confirmDevice->serial_T.write(QByteArray::fromHex("EB900500000000000000000000000000000000")); // 读flash


#if 1
    if (mProgressTSC == nullptr)
    {
        mProgressTSC = new QProgressDialog();
        mProgressTSC->setWindowModality(Qt::ApplicationModal);   // 对本程序模态
        mProgressTSC->setCancelButtonText(nullptr);
        mProgressTSC->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowMinimizeButtonHint);
        mProgressTSC->setLabelText("The Flash Data is Downloading...");
        mProgressTSC->setRange(0, 100);
        mProgressTSC->setValue(0);
        mProgressTSC->setAutoReset(true);
        mProgressTSC->setAutoClose(true);
        mProgressTSC->show();
        //connect(m_pDialog, SIGNAL(canceled()), this, SLOT(cancelSlot()));
        disconnect(mProgressTSC, SIGNAL(canceled()), mProgressTSC, SLOT(cancel()));
    }
#endif
}



// 目前确保 数据正常了
void DeviceConfigTab::recvFlashDataFromTSC(QByteArray arrDataHex)
{
    qDebug() << "11111111111111" << arrDataHex;
    QList<QByteArray> listArrData;
    QList<QByteArray> listArrDataFinal;
    if (arrDataHex.contains("fefefe"))
    {
        bOK = true;
    }
    if (arrDataHex.startsWith("fcfcfc"))
    {
        arrDataHex.remove(0, 6);
    }
    if (arrDataHex.contains("01010154494d"))
    {
        listArrDataFinal = ByteSplit(arrDataHex, "01010154494d");
    }
    for (auto& arrData : listArrDataFinal)
    {
        if (arrData.contains("010101535450"))
        {
            listArrDataFinal.removeOne(arrData);
            listArrData.append(ByteSplit(arrData, "010101535450"));
            listArrData.removeLast();
            arrData.resize(0);
            arrData.append(listArrData.first());
            bOK = true;
        }
        int iSize = arrData.size();
        m_iStartTime = 12 > arrData.size() ? m_iStartTime : QDateTime::fromString("20" + arrData.left(12), "yyyyMMddhhmmss").toTime_t();
        m_iStartTime = QDateTime::fromString("20" + arrData.left(12), "yyyyMMddhhmmss").toTime_t();
        for (int iPos = 12; iPos < arrData.size(); iPos += 6)
        {
            auto&& arrTemp = arrData.mid(iPos, 6);
            auto iTemperature = arrTemp.toUInt(nullptr, 16);
            double dH = iTemperature * 1.0 / pow(2, 24);
            double dN = log(pow(2, 24) / iTemperature - 1);
            double dTemperature = 1 / (m_TSC[0] + m_TSC[1] * dN + m_TSC[2] * pow(dN, 2) + m_TSC[3] * pow(dN, 3)) - 273.15;

            m_vecH.push_back(dH);
            m_vecTemperature.push_back(dTemperature);

            mProgressTSC->setValue(100 * ++iBarVal / (m_iPageQuantityTSC * 169)); // 一页169个有效数据,一个有效数据占3个字节，一页 168*3 = 507 个字节加上时间(年月日时分秒6个字节)=513个字节
            m_vecDateTime.push_back(m_iStartTime);
            m_iStartTime += m_iInterval;
        }
    }

    if (bOK)
    {
        bOK = false;

        mProgressTSC->setValue(100);
        iBarVal = 0;
#if 1
        if (nullptr != mProgressTSC)
        {
            mProgressTSC->hide();
            delete mProgressTSC;
            mProgressTSC = nullptr;
        }
#endif

        qDebug() << "22222222222222" << m_vecH.size();
        qDebug() << "22222222222222" << m_vecDateTime.size();
        qDebug() << "22222222222222" << m_vecTemperature.size();
        // QMessageBox::information(this, "提示信息", "下载完成", QMessageBox::Ok);
        saveDownFileTSC();
    }
}

void DeviceConfigTab::saveDownFileTSC()
{
    // auto&& strFilePath = QFileDialog::getSaveFileName(this, tr("Save download data"), ui->m_labelSerial->text() + "_" + "20" + ui->m_dateTimeEdit->dateTime().toString("yyMMddhhmmss"), tr("DAT File(*.dat);;RAW Files(*.raw)"));
    QString filter = tr("DAT files(*.dat);;RAW files(*.raw)");
    QString strFilePath = QFileDialog::getSaveFileName(this, tr("Save download data"), ui->m_labelSerial->text() +
        "_" + ui->m_dateTimeEdit->dateTime().toString("yyyyMMddhhmmss"), filter);
    QFile clsFile(strFilePath);
    if (strFilePath.isEmpty() || !clsFile.open(QIODevice::WriteOnly))
    {
        m_vecH.clear();
        m_vecDateTime.clear();
        m_vecTemperature.clear();

        m_timer_TSC->start();
        return;
    }
#if 1
    if (NULL == mProgressTSC)
    {
        mProgressTSC = new QProgressDialog();
        mProgressTSC->setWindowModality(Qt::ApplicationModal);   // 对本程序模态
        mProgressTSC->setCancelButtonText(nullptr);
        mProgressTSC->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowMinimizeButtonHint);
        mProgressTSC->setRange(0, 100);
        mProgressTSC->setValue(0);
        mProgressTSC->setAutoClose(true);
        // 设置进度条小于四秒也显示
        mProgressTSC->setMinimumDuration(0);
        mProgressTSC->show();
        disconnect(mProgressTSC, SIGNAL(canceled()), mProgressTSC, SLOT(cancel()));
    }
    mProgressTSC->setLabelText("The Download Data is Saving...");
    mProgressTSC->setValue(0);
#endif
    QString strText = "%Model\t\t\t\t\t\t\t\t\t" + ui->label_type->text() + "\n";
    strText += "%Serial Number\t\t\t\t\t\t\t\t" + ui->m_labelSerial->text() + "\n";
    strText += "%Version Number\t\t\t\t\t\t\t\t" + ui->m_labelHardWare->text() + "\n";
    strText += "%RTC time\t\t\t\t\t\t\t\t" + ui->m_labelDeviceTime->text() + "\n";
    //strText += "%Start time\t\t\t\t\t\t\t\t" + ui->m_dateTimeEdit->text() + "\n";
    strText += "%Start time\t\t\t\t\t\t\t\t" + ui->m_dateTimeEdit->text() + "\n";
    //strText += "%End time\t\t\t\t\t\t\t\t" + ui->m_dateTimeEdit_End->text() + "\n";
    strText += "%End time\t\t\t\t\t\t\t\t" + ui->m_dateTimeEdit_End->text() + "\n";
    strText += "%Immediately?\t\t\t\t\t\t\t\t" + QString(m_bIM ? "Y" : "N") + "\n";
    if (m_iInterval < 60)
    {
        strText += "%Sample interval\t\t\t\t\t\t\t\t" + QString("%1").arg(m_iInterval) + "\n";
        strText += "%Sample interval unit\t\t\t\t\t\t\tSec\n";
    }
    else if (m_iInterval >= 60 && m_iInterval < 3600)
    {
        strText += "%Sample interval\t\t\t\t\t\t\t\t" + QString("%1").arg(m_iInterval / 60) + "\n";
        strText += "%Sample interval unit\t\t\t\t\t\t\tMin\n";
    }
    else if (m_iInterval >= 3600 && m_iInterval <= 17 * 3600)
    {
        int iHour = m_iInterval / 3600;
        int min = (m_iInterval - iHour * 3600) / 60;
        if (min == 0)
        {
            strText += "%Sample interval\t\t\t\t\t\t\t\t" + QString("%1 Hour").arg(iHour) + "\n";
        }
        else {
            strText += "%Sample interval\t\t\t\t\t\t\t\t" + QString("%1 Hour %2 Min").arg(iHour).arg(min) + "\n";
        }
        strText += "%Sample interval unit\t\t\t\t\t\t\tHour\n";
    }
    strText += QString("%Temperature Coefficients\n#%1\n#%2\n#%3\n#%4\n").arg(m_TSC[0], 0, 'R', 15).arg(m_TSC[1], 0, 'R', 15).arg(m_TSC[2], 0, 'R', 15).arg(m_TSC[3], 0, 'R', 15);
    int iSize = m_vecDateTime.size();
    if (strFilePath.endsWith(".dat"))
    {
        strText += QString("%Datetime\t\t\t\t\t\t\tTemperature\n");
        //for (auto iValue : m_vecDateTime)
        for (int index = 0; index < iSize; index++)
        {
            strText += QDateTime::fromTime_t(m_vecDateTime.at(index)).toString("yyyy-MM-dd hh:mm:ss") + QString("\t\t%1\n").arg(m_vecTemperature.at(index), 0, 'R', 4);
            clsFile.write(strText.toUtf8());
            strText.clear();
            mProgressTSC->setValue(100 * index / iSize);
        }
    }
    if (strFilePath.endsWith(".raw"))
    {
        strText += QString("%Datetime\t\t\t\t\t\t\tTemperature\n");
        for (int index = 0; index < iSize; index++)
        {
            strText += QDateTime::fromTime_t(m_vecDateTime.at(index)).toString("yyyy-MM-dd hh:mm:ss") + QString("\t\t%1\n").arg(m_vecH.at(index)*13, 0, 'R', 15);
            clsFile.write(strText.toUtf8());
            strText.clear();
            mProgressTSC->setValue(100 * index / iSize);
        }
    }
#if 1
    if (NULL != mProgressTSC)
    {
        mProgressTSC->setValue(100);
        mProgressTSC->hide();
        delete mProgressTSC;
        mProgressTSC = NULL;
    }
#endif
    clsFile.close();
    qDebug() << "111111111" << m_vecH.size();
    qDebug() << "111111111" << m_vecDateTime.size();
    qDebug() << "111111111" << m_vecTemperature.size();
    m_vecH.clear();
    m_vecDateTime.clear();
    m_vecTemperature.clear();
    qDebug() << "111111111" << m_vecH.size();
    qDebug() << "111111111" << m_vecDateTime.size();
    qDebug() << "111111111" << m_vecTemperature.size();

    qDebug() << "111111111" << m_vecH.capacity();
    qDebug() << "111111111" << m_vecDateTime.capacity();
    qDebug() << "111111111" << m_vecTemperature.capacity();
    // 开启时钟
    m_timer_TSC->start();

}

QList<QByteArray> DeviceConfigTab::ByteSplit(QByteArray& arrByte, const QByteArray& arrSplit)
{
    QList<QByteArray> listArrData;
    while (true)
    {
        auto iPos = arrByte.indexOf(arrSplit);
        if (0 > iPos)
        {
            break;
        }
        else if (0 < iPos)
        {
            listArrData.push_back(arrByte.left(iPos));
        }
        arrByte.remove(0, iPos + 12);
    }

    listArrData.push_back(arrByte);
    return std::move(listArrData);
}

void DeviceConfigTab::on_m_Use_Last_Config_clicked()
{
	// 读取配置
	confirmDevice->serial_T.write(QByteArray::fromHex("EB901000000000000000000000000000000000")); // 475449
}

void DeviceConfigTab::recvSampleEnableStatus(bool flag)
{
    if (!flag)
    {
        // 按钮失效
        setSampleStatusTwo(flag);
        ui->m_pushButton_StartSample->setEnabled(flag);
    }
    else {
        ui->m_pushButton_StartSample->setEnabled(flag);
        setSampleStatusFlagFour(flag);
        // 读取一下配置,可写可不写
        on_m_Use_Last_Config_clicked();
    }


}

void DeviceConfigTab::on_m_radioButton_Hour_clicked(bool checked)
{
    if (checked)
    {
        ui->m_spinBox_Hour->setEnabled(checked);
        ui->m_spinBox_Minute->setEnabled(checked);
    }
    ui->m_spinBox_Second->setEnabled(!checked);
}


void DeviceConfigTab::on_m_radioButton_Minute_clicked(bool checked)
{
    if (checked)
    {
        ui->m_spinBox_Minute->setEnabled(checked);
    }
    ui->m_spinBox_Hour->setEnabled(!checked);
    ui->m_spinBox_Second->setEnabled(!checked);
}


void DeviceConfigTab::on_m_radioButton_Second_clicked(bool checked)
{
    if (checked)
    {
        ui->m_spinBox_Hour->setEnabled(!checked);
        ui->m_spinBox_Minute->setEnabled(!checked);
    }
    ui->m_spinBox_Second->setEnabled(checked);
}


void DeviceConfigTab::on_m_Button_SyncLocalTime_clicked()
{
    QString dateTime = QDateTime::currentDateTime().toString("yyMMddhhmmss");
    QString sendTime = "EB9002" + dateTime + "00000000000000000000";
    confirmDevice->serial_T.write(QByteArray::fromHex(sendTime.toLatin1()));
}


void DeviceConfigTab::on_m_radio_LocalDateTime_clicked(bool checked)
{
    if (checked)
    {
        QDateTime time;
        time = QDateTime::fromString(LocalDateTime, "yyyy-MM-dd hh:mm:ss");
        ui->m_dateTimeEdit->setDateTime(time);
        ui->m_dateTimeEdit->setEnabled(false);
    }
    else
    {
        ui->m_dateTimeEdit->setEnabled(true);
    }
    
}

void DeviceConfigTab::reciveComNameTSC(QString& portName)
{
    qDebug() << "portName = " << portName;
    // 将状态置灰
    setSampleStatus(false);
    ui->m_labelStatus->setText(" 串口拔出");

    // 将T-SC的定时器关闭
    m_timer_TSC->stop();

}

